<template>
  <div>
    <BasicModal
      @register="register"
      :title="title"
      width="600px"
      :destroyOnClose="true"
      :canFullscreen="false"
      :showOkBtn="false"
      :showCancelBtn="false"
    >
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { ref } from 'vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import Dictionary from '@/utils/dictionary';
  import { DictDataSave, DictDataUpdate } from '@/api/platform/dictionary';
  import { IDictDataSaveData } from '@/api/platform/dictionary/type';

  const title = ref('新增字典数据');
  const type = ref(1);
  const { createMessage } = useMessage();
  const dicts = Dictionary.getLocalMulti(['dictStatus']);
  const dictType = ref();

  const emit = defineEmits(['reload']);

  const typeMap = {
    1: '新增字典数据',
    2: '编辑字典数据',
  };

  interface formData {
    userVersion: string;
    userDesc: string;
    templateId: number;
    tenantIds: number[];
  }

  const form = ref<formData>({
    userVersion: '',
    userDesc: '',
    templateId: 0,
    tenantIds: [],
  });

  const [registerForm, { validate, setProps, setFieldsValue }] = useForm({
    schemas: [
      {
        field: 'dictCode',
        component: 'Input',
        label: 'dictCode',
        show: false,
      },
      {
        field: 'dictLabel',
        component: 'Input',
        label: '字典标签',
        required: true,
      },
      {
        field: 'dictValue',
        component: 'Input',
        label: '字典键值',
        required: true,
      },
      {
        field: 'dictSort',
        component: 'Input',
        label: '字典排序',
        required: true,
      },
      {
        field: 'status',
        component: 'Select',
        label: '状态',
        required: true,
        componentProps: {
          fieldNames: { label: 'dictLabel', value: 'dictValue' },
          options: dicts.dictStatus,
        },
      },
      {
        field: 'remark',
        component: 'InputTextArea',
        label: '备注',
        componentProps: {
          rows: 4,
        },
      },
    ],
    autoFocusFirstItem: true,
    actionColOptions: {
      span: 24,
    },
    labelWidth: 120,
    baseColProps: { span: 18 },
    submitButtonOptions: {
      text: '确认',
    },
    resetButtonOptions: {
      text: '取消',
    },
    submitFunc: async () => {
      const data: IDictDataSaveData = await validate();
      setProps({
        submitButtonOptions: {
          loading: true,
        },
      });
      try {
        data.dictType = dictType.value;
        console.log(data, form.value);
        (data.dictCode && (await DictDataUpdate({ ...data }))) || (await DictDataSave({ ...data }));
        setProps({
          submitButtonOptions: {
            loading: false,
          },
        });

        createMessage.success('提交成功！');
        emit('reload');
        closeModal();
      } catch (error) {
        console.log('error--->', error);
        setProps({
          submitButtonOptions: {
            loading: false,
          },
        });
      }
    },
    resetFunc: async () => {
      closeModal();
    },
  });

  const [register, { closeModal }] = useModalInner(async (data) => {
    console.log('data--->', data);
    type.value = data.type;
    dictType.value = data.dictType;

    data.row && setFieldsValue(data.row);

    title.value = typeMap[data.type];
  });
</script>

<style lang="less" scoped></style>
